package com.ai.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.web.service.BgRoleService;
import com.ai.web.vo.BgRoleV;
import com.ai.web.vo.QueryData;
import com.ai.web.vo.ResultModel;
/**
 * 微信前端角色控制层
* @ClassName: BgroleController 
* @Description: TODO 
* @author:  
* @date: 2014年3月24日
*
 */
@Controller
@RequestMapping("/bgRole")
public class BgRoleController  extends BaseController {
    

    private static Logger logger = Logger.getLogger(BgRoleController.class);
    
    @Autowired
    private BgRoleService bgRoleService;
    
    @RequestMapping(value = "/list")
    public String list() {
        
        logger.info("hello......test");
        return "bgRole/bgRole_list";
    }
    
    @RequestMapping(value = "/asignMenu")
    public String asignMenu() {
         return "bgRole/asignMenu";
    }
    
    @RequestMapping(value = "/roleList")
    @ResponseBody
    public List<BgRoleV> getroleList()
            throws Exception {
        return bgRoleService.getRoleList();
    }
    
    @RequestMapping(value = "/query", produces = "application/json")
    @ResponseBody
    public QueryData query(@RequestParam HashMap<String, Object> paramMap)
            throws Exception {
        
        Map<String, Object> conditionMap = buildCondition(paramMap);
        logger.info("---- " + conditionMap);
        
        QueryData qd = bgRoleService.queryListAndCount(conditionMap);
        
        return qd;
    }
    
    @RequestMapping(value = "/new")
    public String newBgrole(Model model) {
        
        BgRoleV BgroleV = new BgRoleV();
        model.addAttribute(BgroleV);
        
        return "bgRole/bgRole_template";
    }
    
    @RequestMapping(value = "/update/{roleId}")
    public String update(@PathVariable("roleId") int roleId, Model model)
            throws Exception {
        
        BgRoleV bgRoleV = bgRoleService.getRole(roleId);
        logger.info("----- getUser:" + bgRoleV);
        
        if (bgRoleV == null) {
            // exception redirect error page
            // TODO
        }
        model.addAttribute(bgRoleV);
        
        return "bgRole/bgRole_template";
    }
    
    @RequestMapping(value = "/save", produces = "application/json")
    @ResponseBody
    public ResultModel saveBgRole(BgRoleV bgRoleV) throws Exception {
        
        logger.info("-----save:" + bgRoleV);
                
        ResultModel rm = bgRoleService.saveRole(bgRoleV);
        
        
        return rm;
    }
    
    @RequestMapping(value = "/delete", produces = "application/json")
    @ResponseBody
    public ResultModel deleteBgRole(@RequestParam String delIds)
            throws Exception {
        
        System.out.println(delIds);
        
        List<Integer> Ids = getDeleteIds(delIds);
        ResultModel rm = bgRoleService.deleteRoles(Ids);
        
        return rm;
    }
    
   
}
